package com.wzz.server.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wzz.auth.cache.Cache;

import sun.misc.Contended;

@Controller
public class AuthController {
	
	@Autowired
	Cache cache;

	@RequestMapping("/server/accept")
	public String dealServer(String response_type, String client_id, String redirect_uri, String scope, String state, 
			HttpServletRequest request, HttpServletResponse response) {
		
		System.out.println("---------------------------------------");
		System.out.println("接收到认证申请...");
		System.out.println("response_type="+response_type);
		System.out.println("client_id="+client_id);
		System.out.println("redirect_uri="+redirect_uri);
		System.out.println("scope="+scope);
		System.out.println("state="+state);
		
		System.out.println("------->用户同意，接着以下操作。。。");
		
		request.setAttribute("response_type", response_type);
		request.setAttribute("client_id", client_id);
		request.setAttribute("scope", scope);
		request.setAttribute("name", state);
		request.setAttribute("redirect_uri", redirect_uri);
		
		System.out.println("---------------------------------------");
		return "auth";
		
		
	}
	
	@RequestMapping("/server/dealUser")
	public void dealUser(String name, String redirect_uri, String scope,  HttpServletResponse response) {
		String code = "123456";
		cache.getMap().put(code, redirect_uri);
		try {
			System.out.println("---------------");
			System.out.println("发送授权码");
			System.out.println("name="+name);
			response.sendRedirect(redirect_uri+"?code="+code+"&name="+name);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/server/token")
	public void dealToken(HttpServletRequest request, HttpServletResponse response, 
			String grant_type, String code, String redirect_uri, String client_id, String state) {
		System.out.println("-------------------------------");
		System.out.println("请求token来了...");
		System.out.println("grant_type="+grant_type);
		System.out.println("code="+code);
		System.out.println("redirect_uri="+redirect_uri);
		System.out.println("client_id="+client_id);
		if("123456".equals(code)){
			try {
				response.sendRedirect(redirect_uri+"?access_token="+"token123&state="+state);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		System.out.println("-------------------------------");
	}
}
